//
//  HomeCentenView.m
//  ScalperMerchant
//
//  Created by 王云 on 2017/9/29.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "HomeCentenView.h"
#import "SCUser.h"

@interface HomeCententButton : UIButton

@property (nonatomic,weak)UIImageView *iconImg;
@property (nonatomic,weak)UILabel *titleL;

@end
@implementation HomeCententButton

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        UIImageView *iconImg = [[UIImageView alloc] init];
        iconImg.image = [UIImage imageNamed:@"5"];
        [self addSubview:iconImg];
        _iconImg = iconImg;
        
        UILabel *titleL = [[UILabel alloc] init];
        titleL.text = @"核销";
        [titleL setTextAlignment:NSTextAlignmentCenter];
        titleL.font = [UIFont systemFontOfSize:14];
        [self addSubview:titleL];
        _titleL = titleL;
        
        __weak __typeof(&*self)weakSelf = self;
        [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top).offset(8);
            make.centerX.mas_equalTo(weakSelf);
            make.size.mas_equalTo(CGSizeMake(55, 55));
        }];
        [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(weakSelf);
            make.top.equalTo(iconImg.mas_bottom).offset(6);
        }];
    }
    return self;
}

@end


@interface HomeCentenView ()

@property (nonatomic,weak)UILabel *nameL;

//@property (nonatomic,weak)UIImageView *iconImg;
@property (nonatomic,weak)UILabel *typeL;


@property (nonatomic,weak)HomeCententButton *leftBtn;
@property (nonatomic,weak)HomeCententButton *centenBtn;
@property (nonatomic,weak)HomeCententButton *myBtn;

@end

@implementation HomeCentenView

- (instancetype)initWithFrame:(CGRect)frame option:(Option)option{
    HomeCentenView *CentenV  = [[HomeCentenView alloc] initWithFrame:frame];
    CentenV.option = option;
    return CentenV;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    UILabel *nameL = [[UILabel alloc] init];
    nameL.text = @"请求有误";
    [nameL setTextAlignment:NSTextAlignmentCenter];
    nameL.font = [UIFont systemFontOfSize:14];
    [self addSubview:nameL];
    _nameL = nameL;
    
    UILabel *typeL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 56, 18)];
    typeL.text = @"正在审核";
    [typeL setTextAlignment:NSTextAlignmentCenter];
    typeL.layer.cornerRadius = 3;
    typeL.layer.masksToBounds = YES;
    typeL.font = [UIFont systemFontOfSize:12];
    typeL.textColor = [UIColor whiteColor];
    typeL.backgroundColor = KGreenColor;
    [self addSubview:typeL];
    _typeL = typeL;
    
    UIImageView *icomImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    icomImg.image = [UIImage imageNamed:@"dianpu"];
    [self addSubview:icomImg];
    
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgV];
    
    HomeCententButton *leftBtn = [[HomeCententButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 100)];
    leftBtn.tag = BtnWrite;
    leftBtn.iconImg.image = [UIImage imageNamed:@"hexiao"];
    leftBtn.titleL.text = @"核销";
    [leftBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftBtn];
    _leftBtn = leftBtn;
    
    HomeCententButton *centenBtn = [[HomeCententButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 100)];
    centenBtn.tag = BtnBill;
    centenBtn.iconImg.image = [UIImage imageNamed:@"home_zhangdan"];
    centenBtn.titleL.text = @"我的账单";
    [centenBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:centenBtn];
    _centenBtn = centenBtn;

    HomeCententButton *myBtn = [[HomeCententButton alloc] initWithFrame:CGRectMake(0, 0, kWidth/3, 100)];
    myBtn.tag = BtnMy;
    myBtn.iconImg.image = [UIImage imageNamed:@"wde"];
    myBtn.titleL.text = @"我的";
    [myBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:myBtn];
    _myBtn = myBtn;
    
    __weak __typeof(&*self)weakSelf = self;
    [nameL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf);
        make.top.equalTo(weakSelf.mas_top).offset(6);
        make.height.equalTo(@30);
    }];
    [typeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(nameL.mas_centerY);
        make.left.equalTo(nameL.mas_right).offset(6);
        make.size.mas_equalTo(typeL.frame.size);
    }];
    [icomImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(nameL.mas_centerY);
        make.right.equalTo(nameL.mas_left).offset(-6);
        make.size.mas_equalTo(icomImg.frame.size);
    }];
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf);
        make.top.equalTo(nameL.mas_bottom).offset(6);
    }];
    
    NSMutableArray *btnArr = [NSMutableArray array];
    [btnArr addObjectsFromArray:@[leftBtn,centenBtn,myBtn]];
    
    [HWCommon makeEqualWidthViews:btnArr inView:bgV LRpadding:0 viewPadding:0];
}

- (void)btnClick:(UIButton *)btn{
    
    switch (btn.tag) {
        case BtnWrite:
        {
            if (_option) {
                _option(BtnWrite);
            }
        }
            break;
        case BtnBill:
        {
            if (_option) {
                _option(BtnBill);
            }
        }
            break;
        case BtnMy:
        {
            if (_option) {
                _option(BtnMy);
            }
        }
            break;
            
        default:
            break;
    }
    
}

- (void)setUser:(SCUser *)user{
    _user = user;
    if ([_user.joinin_state isEqualToString:@"10"]) {
        _typeL.textColor = [UIColor whiteColor];
        _typeL.backgroundColor = KGreenColor;
        _typeL.text = @"正在审核";
    }else{
        _typeL.textColor = [UIColor whiteColor];
        _typeL.backgroundColor = KGreenColor;
        _typeL.text = @"审核通过";
    }
    _nameL.text = _user.store_name;
}


@end
